extends Skill




@onready var ske = $AS

## 技能范围
var rang: int = 2 # 范围
var igs: Array = [Vector2(0, 0)] # 忽略指
var mode := CreateGrid.CREATE_GRID_MODE.CROSS # 范围模式
## 技能效果
var num: int = 1 # 伤害加成
var direction: Vector2 = Vector2.ZERO # 释放方向
var targets: Array[Unit] = []

## 技能发动代价
var cost_ep: int = 2
var cost_attack_chance: int = 1
var cost_action_chance: int = 1

## 技能[显示图层]与[点击图层]的下标
var area_show: int = -1
var area_call: int = -1

## 这个技能是一个选一定范围内的一个格子,并对这个方向进行攻击
func start(_args: Dictionary = {}) -> void:
	# 获取游戏场景
	var sce: Scene = get_scene()
	var off := sce.map.local_to_map(_args["user"].position)
	var pos := GCreateGrid.create_grid(mode, rang, igs, off)
	var lpos = []
	
	for index in range(0, pos.size()):
		lpos.append(sce.map.map_to_local(pos[index]))
	
	# 生成格子
	# 用于显示
	area_show = sce.select_manage.show_range(
		lpos,
		SceneSelectManage.SelectMangerMode.NONE
	)[0]
	# 更改个颜色
	var layer := sce.select_manage.get_layer_index(area_show)
	for a in layer["areas"]:
		a.modulate = Color8(255, 255, 255, 127)
	
	# 生成格子
	# 用于选择
	var pos2 := GCreateGrid.create_grid(mode, 1, igs, off)
	lpos.clear()
	for index in range(0, pos2.size()):
		lpos.append(sce.map.map_to_local(pos2[index]))
	area_call = sce.select_manage.show_range(
		lpos,
		SceneSelectManage.SelectMangerMode.SKILL,
		call_check,
		_args
	)[0]
	sce.select_manage.set_layer_clear_callable(area_call, call_clear)




## 清除回调
func call_clear(_args: Dictionary) -> bool:
	var sce: Scene = get_scene()
	sce.select_manage.remove_layer_int(area_call, false)
	sce.select_manage.remove_layer_int(area_show, false)
	#print("[Penetrate]回调 >> _args >>", _args)
	return false




## 回调检查
func call_check(_args: Dictionary) -> bool:
	# 用回调的位置来判断是否有单位，有就增加HP
	var tp = _args["target_position"]
	# 在点击范围外的对方时，就取消这个技能的发动
	if  tp == null:
		return true
	
	# 如果点击了有效位置
	# 就判断位置上是否是一个 unit
	else:
		var sce: Scene = get_scene()
		# 向量的归一值，获取延伸位置
		var mmpos = get_master_unit().position
		var n: Vector2 = mmpos.direction_to(tp) * sce.map.cell_quadrant_size
		var pt: Array[Vector2] = []
		for i in range(0, rang):
			var next = mmpos + (i + 1) * n
			#print("next > ", next)
			pt.append(next)
		
		var u = sce.unit_manage.get_position_units(pt)
#		print("pt >", pt)
#		print("u > ", u)
#		for v in u:
#			print(v.position)
		if not u.is_empty():
			targets = u
			direction = n
			_run()
		return true
	
	return false




## 需要消耗一个攻击机会
func cost() -> bool:
	if not is_instance_valid(master):
		return false;
	
	# 因为效果的父节点是 UnitSkill
	# 所以访问 Unit 的 master 来获取 unit
	var property: UnitProperty = get_master_unit().property
	if  property.action_chance >= cost_action_chance and \
		property.attack_chance >= cost_attack_chance and \
		property.current_ep >= cost_ep:
		return true
		
	return false




func _run() -> void:
	var mm: Unit = get_master_unit()
	var sh: int = mm.property.ap_ordinary + 1
	
	# 支付代价
	mm.property.current_ep -= 2
	mm.property.action_chance -= 1
	mm.property.attack_chance -= 1
	
	for i in range(0, targets.size()):
		if targets[i] == null:
			continue
		if i != 0:
			var a: int = sh * 0.25
			if a <= 2:
				sh -= 2
			else:
				sh -= a
		targets[i].property.under_attack(mm, {
			"type": "ordinary",
			"harm": sh
		})
	
	var origin: Vector2 = mm.icon.global_position
	var tween: Tween = get_tree().create_tween()
	tween.tween_property(mm.icon, "global_position", mm.icon.global_position + direction / 2, 0.25)
	
	ske.play("default")
	ske.position = mm.position
	ske.look_at(mm.position + direction)
	ske.position += direction * 2
	ske.show()
	await ske.animation_finished
	ske.hide()
	ske.position = Vector2(0, 0)
	
	tween = get_tree().create_tween()
	tween.tween_property(mm.icon, "global_position", origin, 0.25)
